/**
 * 使用BBB的数据结构
 */


class OutMessage2x {
	constructor(messageName, routing, headerFields) {
		messageName = messageName ? messageName : this.constructor.name
		this.envelope = {
			name: messageName,
			routing: routing,
			timestamp: Date.now(),
		}
		/**
		 * The header template of the message
		 * @type {Object}
		 */
		this.core = {
			header: {
				name: messageName
			},
			body: null
		}

		// Copy header fiels to the header object
		var keys1 = Object.keys(headerFields);
		for (var k = 0; k < keys1.length; k++) {
			var key = keys1[k];
			if (typeof this.core.header[key] === 'undefined') {
				this.core.header[key] = headerFields[key];
			}
		}

		/**
		 * Generates the JSON representation of the message
		 * @return {String} The JSON string of this message
		 */
		this.toJson = function() {
			return JSON.stringify(this);
		}
	}
}

module.exports = OutMessage2x;
